// pages/login/login.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
  //登陆判断
  datalogin:null,
  infoss:'',
  token:null,
  userlist:[],
  code:null,
  token:null,
  userInfo:[],
  },
  /* 小程序1.0版本
   * 选择店铺后返回上一级
   */
  navigateBack() {
    wx.switchTab({
      url: '../index/index',
    })
  },
  //登录
  login:function() {
    let that =this
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res.userInfo)
        //提交到服务器进行绑定
        that.eidtuserinfo()
        wx.setStorageSync('wxinfo',res.userInfo);
        that.setData({
          userlist:res.userInfo
        })
        //进行微信登陆判断企业登陆或者微信用户登陆
        wx.login({
          success (res) {
              that.setData({
                code:res.code
              })
              //重新缓存用户名
             let datalogins = that.data.datalogin
              if(datalogins!=undefined){
                //企业登陆
                that.qiyelogin()
              }else{
                //个人登陆    
                that.wxlogin()
              }
           
          }
        })
      },
      fail:(res)=> {
        wx.showToast({
          title: '请先获取用户信息',
          icon: 'error',
          duration: 2000
        })
        
      }
    })
  },
  //个人登陆
  wxlogin:function() {
    let that = this
    let code = that.data.code
    wx.request({
                url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
                data: {
                  code: code,
                  cmd: 'wxlogin',
                  officeid:1
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success (data) {
                  console.log(data)
                  if(data.data.STATUS=='Success'){
                    wx.setStorageSync('userInfo',data.data.DATA);
                    wx.setStorageSync('token',data.data.DATA.token);
                    that.setData({
                      token:data.data.DATA.token,
                      userInfo:data.data.DATA
                    })
                    if(data.data.DATA.companyName==''){
                      that.eidtuserinfo()
                    }
                      wx.switchTab({
                        url: '../index/index',
                      })
                  }
                }
              });
  },
  //企业登陆
  qiyelogin:function() {
    let that = this
    let code = that.data.code
    //let e = JSON.parse(that.data.datalogin);
    let e =    wx.getStorageSync('datalogins')  
    wx.request({
                url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
                data: {
                  cmd: 'login',
                  officeid:1,
                  code: code,
                  employeeId:e.employeeId,
                  employeeName:e.employeeName,
                  companyId:e.companyId,
                  companyName:e.companyName,
                  mobile:e.mobile,
                  role:e.role,
                  token:e.token,
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success (data) {
                  console.log(data)
                  if(data.data.STATUS=='Success'){
                    wx.setStorageSync('userInfo',data.data.DATA);
                    wx.setStorageSync('token',data.data.DATA.token);
                    that.setData({
                      token:data.data.DATA.token,
                      userInfo:data.data.DATA
                    })
                      wx.switchTab({
                        url: '../index/index',
                      })
                  }
                }
              });
  },
  //更新用户信息
  eidtuserinfo(){
    let that = this
    let userlist = that.data.userlist
    let token =that.data.token
    let userInfo = that.data.userInfo
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        cmd: 'UpdateGuestInfo',
        token:token,
        mobile:'',
        GuestNm:'',
        nickName:userlist.nickName,
        avatarUrl:userlist.avatarUrl,
        province:userlist.province,
        city:userlist.city,
        gender:userlist.gender,
        Email:'',
        CorpName:userlist.nickName
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (data) {
        console.log(data)
      }
    });
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let that = this
      //console.log(options)
      that.setData({
        datalogin:options.datalogin
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})